$(function(){
	exit();
	//type();
})

function type(){
	$("#braId").html("");
	var url="../../branch/selectAll.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#braId").append("<option value=''>====请选择====</option>");
		$.each(mes,function(index,item){
			$("#braId").append("<option value="+item.braId+">"+item.sort+"</option>");
			load();
		});
	},"json");
}


function exit(){
	$("#braId").val("");
  	$("#braName").val("");
	$("#braSite").val("");
	$("#braExplain").val("");
	$("#isva").val("");
	$("#optime").val("");
	$("#oper").val("");
	load();
	//关闭左右页面层
	layer.closeAll('page');
}
layui.use(['form', 'layedit', 'laydate','table'], function(){
  var table = layui.table
  ,laydate = layui.laydate
  ,form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,upload = layui.upload;
  
  
 
  //监听工具条
  table.on('tool(demo)', function(obj){
    var da = obj.data;
    if(obj.event === 'edit'){
    	var url="../../branch/findById.action";
        var data={"braId":da.braId};
        $.post(url,data,function(mes){
        	layer.open({
    	        type: 1	 //此处以iframe举例
    	        ,title: '数据修改'
    	        ,area: ['680px', '490px']
        		,offset: 'auto'
    	        ,content:$('#addOrUpdate')
    	        ,success: function(layero){
    	        	$("#braId").val(mes.braId);
    	          	$("#braName").val(mes.braName);
    	        	$("#braSite").val(mes.braSite);
    	        	$("#braExplain").val(mes.braExplain);
    	        	//$("#isva").val(mes.isva);
    	        	$("#optime").val(mes.optime);
    	        	$("#opers").val(mes.oper);
    	        	$("#braId").val(mes.braId);
    	        	//$("#oper").attr({"disabled":true});
    	        	//渲染
    	        	load();
        		},end : function() {
        			//$("#oper").attr({"disabled":false});
    	        	//弹框关闭的回调函数
    				$("#addOrUpdate").hide();
    				exit();
    			}
    	    });
        	
        },"json");
    }else if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
        	var url="../../branch/delete.action";
        	var data={"braId":da.braId}
        	$.post(url,data,function(mes){
        		if(mes==1){
        			obj.del();
                    layer.close(index);
                    layer.msg("删除成功");
        		}else{
        			layer.msg("删除失败");
        		}
        	},"json");
            
          });
        }else if(obj.event=='audit'){
			layer.confirm('请确认提交审核',{title:"确认操作"}, function(index){

				//提交审核
				var url="../../branchAudit/addBranch.action";
				var data={"braId":da.braId};
				$.post(url,data,function(mes){
					if(mes==1){
						layer.msg("提交成功,请耐心等待");
						//重载表单
						active['reload'] ? active['reload'].call(this) : '';
					}else{
						layer.msg("提交失败");
					}
				},"json");
			});
		}
  });
  
  /**
   * 查询
   */
  var $ = layui.$, active = {
		    reload: function(){
		      var braName = $('#braName');
		      var sort= $('#sort');
		      //alert(state.val());
		      //执行重载
		      table.reload('idTest', {
		        page: {
		          curr: 1 //重新从第 1 页开始
		        }
		        ,where: {
		        	braName: braName.val(),
		        	sort:sort.val()
		        }
		      });
		    }
		  };
		  
		  $('.demoTable .layui-btn').on('click', function(){
		    var type = $(this).data('type');
		    active[type] ? active[type].call(this) : '';
		  });
		  
		  $('#add').on('click', function(){
			  layer.open({
	    	        type: 1	 //此处以iframe举例
	    	        ,title: '数据增加'
	    	        ,offset: 'auto'
	    	        ,area: ['680px', '390px']
	    	        ,content:$('#addOrUpdate')
	    	        ,end : function() {
	    	        	//弹框关闭的回调函数
	    				$("#addOrUpdate").hide();
	    				$('#myform')[0].reset();
	    				//alert($('#opers').val())
	    				//$('#opers').val("");
	    				exit();
	    			}
	    	    });
		  });
		  $('#sub').on('click', function(){
			  var url="../../branch/addOrUpdate.action";
			  var data=$("#myform").serialize();
			  $.post(url,data,function(mes){
				  if(mes==1){
					  $("#addOrUpdate").hide();
					  exit();
					  parent.layer.msg('操作成功', {
					      icon: 1
					  });
					  //重载表单
					  active['reload'] ? active['reload'].call(this) : '';
				  }else{
					  parent.layer.msg('操作失败', {
					      icon: 2
					  });
				  }
			  },"json");
		  });
		  
});
function load() {
	 var form = layui.form
	 form.render();
}